package com.example.uploadattachment.sftp;

import com.jcraft.jsch.ChannelSftp;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;

/**
 * @author 何逸诚
 * @date 2025/3/19 8:30
 * @description SFTP连接池配置
 */
@Configuration
public class SftpPoolConfig {
    @Value("${sftp.pool.maxTotal}")
    private int maxTotal;
    @Value("${sftp.pool.maxIdle}")
    private int maxIdle;
    @Value("${sftp.pool.minIdle}")
    private int minIdle;
    @Value("${sftp.pool.maxWaitMillis}")
    private long maxWaitMillis;

    @Bean
    public GenericObjectPoolConfig<ChannelSftp> poolConfig() {
        GenericObjectPoolConfig<ChannelSftp> config = new GenericObjectPoolConfig<>();
        config.setMaxTotal(maxTotal);
        config.setMaxIdle(maxIdle);
        config.setMinIdle(minIdle);
        config.setMaxWait(Duration.ofMillis(maxWaitMillis));
        config.setTestOnBorrow(true);
        config.setTestOnReturn(true);
        config.setTimeBetweenEvictionRuns(Duration.ofSeconds(30));
        return config;
    }
}
